package wareHouse;


import logIn.MysqlConnectionTool;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
 * 入库功能实现
 * @Author Jian Chen long
 * @Date 2021/6/16 14:29
 */
public class InHouse extends JFrame {
    private JLabel label = new JLabel("商品入库系统");
    private JPanel panel = new JPanel();
    private JPanel panel1 = new JPanel(new GridLayout(5,1,1,1));

    private JLabel modelLabel = new JLabel("商品型号");
    private JTextField modelText = new JTextField(15);

    private JLabel inBrandLabel = new JLabel("品牌");
    private JTextField inBrandText = new JTextField(15);

    private JLabel inCountLabel = new JLabel("数量");
    private JTextField inCountText = new JTextField(15);

    private JLabel inPriceLabel = new JLabel("单价");
    private JTextField inPriceText = new JTextField(15);

    private JPanel panel2 = new JPanel(new GridLayout(1,1,0,0));
    private JButton inButton = new JButton("确定");
    private JButton cancelButton = new JButton("返回");

    public InHouse(){
        panel.add(label);
        panel1.add(modelLabel);
        panel1.add(modelText);
        panel1.add(inBrandLabel);
        panel1.add(inBrandText);
        panel1.add(inCountLabel);
        panel1.add(inCountText);
        panel1.add(inPriceLabel);
        panel1.add(inPriceText);
        panel2.add(inButton);
        panel2.add(cancelButton);
        panel.add(panel1);
        panel.add(panel2,BorderLayout.SOUTH);
        this.add(panel);

        this.setTitle("入库系统");
//        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setSize(420,240);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setResizable(false);

        /**
         * 入库确定按钮实现
         */
        inButton.addActionListener(new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                String mode = modelText.getText();
                String brand = inBrandText.getText();
                String count = inCountText.getText();
                String price = inPriceText.getText();


                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;
                try{
                    if (mode.length() == 0 || brand.length() == 0 || count.length() == 0 || price.length() == 0){
                        JOptionPane.showMessageDialog(null,"请填写完整信息");
                        return;
                    }
                    // 程序执行到这,说明信息填写完整无错误
                    //获取连接
                    conn = MysqlConnectionTool.getConnection();
                    String sql = "select inbrand from inhouse ";
                    ps = conn.prepareStatement(sql);
                    rs = ps.executeQuery();
                    while (rs.next()){
                        // 判断商品品牌是否存在
                        if (brand.equals(rs.getString("inbrand")) == true){
                            String sql2 = "select inname from inhouse where inbrand = ?";
                            ps = conn.prepareStatement(sql2);
                            ps.setString(1,brand);
                            rs = ps.executeQuery();
                            while (rs.next()){
                                /**
                                 * 此处存在部分功能性问题:
                                 *          1.更新库存数量和价格后,时间不改变,仍未第一次入库时间
                                 *          2.只能存储一种品牌的一种商品
                                 */
                                // 判断入库商品型号是否已经存在
                                if (mode.equals(rs.getString("inname")) == true ){
                                    // 查询添加库存前,有多少数量,
                                    String selectOldCount = "select incount from inhouse where inname = ?";
                                    ps = conn.prepareStatement(selectOldCount);
                                    ps.setString(1,mode);
                                    rs = ps.executeQuery();
                                    while (rs.next()){
                                        // 原有库存数量加上现入库数量
                                        int newCount = Integer.parseInt(rs.getString("incount")) + Integer.parseInt(count);
                                        // 用于更新数量
                                        String updateInCount = "update inhouse set incount = " + newCount + " where inname = ?";
                                        // 用于更新价格
                                        String updateInPrice = "update inhouse set inprice = " + price + " where inname = ?";
                                        ps = conn.prepareStatement(updateInCount);
                                        ps.setString(1,mode);
                                        int updateInCountFlag = ps.executeUpdate();
                                        ps = conn.prepareStatement(updateInPrice);
                                        ps.setString(1,mode);
                                        int updateInPriceFlag = ps.executeUpdate();
                                        if (updateInCountFlag == 1 && updateInPriceFlag == 1){
                                            JOptionPane.showMessageDialog(null,"入库成功");
                                            // 输入框清空,方便用户
                                            modelText.setText(null);
                                            inBrandText.setText(null);
                                            inCountText.setText(null);
                                            inPriceText.setText(null);
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                    }

                    // 如果代码执行到这里,说明没有某个品牌,或者没有某个的某种型号产品，插入一条全新的信息
                    String sql3 = "insert into inhouse(inname,inbrand,incount,inprice) values(?,?,?,?)";
                    ps = conn.prepareStatement(sql3);
                    ps.setString(1,mode);
                    ps.setString(2,brand);
                    ps.setString(3,count);
                    ps.setString(4,price);
                    int insertFlag = ps.executeUpdate();
                    if (insertFlag == 1){
                        JOptionPane.showMessageDialog(null,"入库成功,新的产品");
                        modelText.setText(null);
                        inBrandText.setText(null);
                        inCountText.setText(null);
                        inPriceText.setText(null);
                        return;
                    }
                }catch (Exception E){
                    JOptionPane.showMessageDialog(null,"入库出错,请重试");
                    return;
                }finally {
                    MysqlConnectionTool.close(conn,ps,rs);
                }

            }
        });
        /**
         * 返回按钮功能实现
         */
        cancelButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 点击返回按钮退出系统
                JOptionPane.showMessageDialog(null,"系统退出");
                System.exit(0);
            }
        });
    }
}
